package com.tools.developer.app;

import java.util.Comparator;

import android.os.Parcel;
import android.os.Parcelable;

public class AppModel implements Parcelable {

	public static final int APP_TYPE_ALL = 0x1001;
	public static final int APP_TYPE_RUNNING = 0x1002;
	public static final int APP_TYPE_SYS = 0x1003;

	public String packageName;
	public String mVer;
	public String appId;
	public String mLabel;
	public String publicSourceDir;
	public String dataDir;
	public String mShareUid;

	public boolean isSystemApp;
	public boolean isRunning;
	public boolean checked;

	public long count = -1;
	public long size;

	public int storage;
	public int pid;

	public double memUsage;

	public static Comparator<AppModel> SORT_APP_BY_MEM = new Comparator<AppModel>() {

		@Override
		public int compare(AppModel object1, AppModel object2) {
			if (object1.checked != object2.checked) {
				return (object1.checked ? -1 : 1);
			}
			double diff = object2.memUsage - object1.memUsage;
			if (object1.checked == object2.checked) {
				if (diff > 0) {
					return 1;
				} else if (diff < 0) {
					return -1;
				} else {
					return 0;
				}
			}
			return 1;
		}

	};

	public static Comparator<AppModel> SORT_APP_BY_LABEL = new Comparator<AppModel>() {

		@Override
		public int compare(AppModel object1, AppModel object2) {

			return object1.mLabel.compareTo(object2.mLabel);

		}

	};

	public AppModel(Parcel source) {
		//主要先后顺序
		packageName = source.readString();
		mVer = source.readString();
		appId = source.readString();
		mLabel = source.readString();
		publicSourceDir = source.readString();
		dataDir = source.readString();
		mShareUid = source.readString();

		isSystemApp = source.readInt() == 1 ? true : false;
		isRunning = source.readInt() == 1 ? true : false;
		checked = source.readInt() == 1 ? true : false;

		count = source.readLong();
		size = source.readLong();

		storage = source.readInt();
		pid = source.readInt();

		memUsage = source.readDouble();

	}

	public AppModel() {
	}

	@Override
	public int describeContents() {
		return 0;
	}

	@Override
	public void writeToParcel(Parcel dest, int flags) {
		//主要先后顺序
		dest.writeString(packageName);
		dest.writeString(mVer);
		dest.writeString(appId);
		dest.writeString(mLabel);
		dest.writeString(publicSourceDir);
		dest.writeString(dataDir);
		dest.writeString(mShareUid);

		dest.writeInt(isSystemApp ? 1 : 0);
		dest.writeInt(isRunning ? 1 : 0);
		dest.writeInt(checked ? 1 : 0);

		dest.writeLong(count);
		dest.writeLong(size);
		
		dest.writeInt(storage);
		dest.writeInt(pid);
		
		dest.writeDouble(memUsage);

	}

	// 实例化静态内部对象CREATOR实现接口Parcelable.Creator
	public static final Parcelable.Creator<AppModel> CREATOR = new Creator<AppModel>() {

		@Override
		public AppModel[] newArray(int size) {
			return new AppModel[size];
		}

		// 将Parcel对象反序列化为ParcelableDate
		@Override
		public AppModel createFromParcel(Parcel source) {
			return new AppModel(source);
		}
	};
}
